﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ServiceModel;
using UploadWcfService;
using System.Configuration;
using iSun.Entity;
using ZX.Common.IPTC;
using System.Windows.Forms;
namespace UpLoadConsoleApplication
{
    class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.Run(new MainFrom());
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {

        }
        //static void Main(string[] args)
        //{
           
        //    iSun.UpLoadServiceClient ser = new iSun.UpLoadServiceClient();
        //    iSun.IUpLoadService channel = ser.ChannelFactory.CreateChannel();
        //    BasicHttpBinding binding2 = new BasicHttpBinding();
        //    binding2.TransferMode = TransferMode.Streamed;

        //    //IUpLoadService channel = ChannelFactory<IUpLoadService>.CreateChannel(binding2,
        //    //new EndpointAddress("http://localhost:1630/UpLoadService.svc"));
        //     string path = ConfigurationManager.AppSettings["filePath"];
        //    IList<string> _filelist = channel.GetFiles();
        //    foreach (string __files in _filelist)
        //    {
        //        iSun.DownloadRequest request = new iSun.DownloadRequest();
        //        request.FileName = __files;
        //        iSun.FileUploadMessage __Data = channel.GetProduct(request);
        //        string __path = ConfigurationManager.AppSettings["sfilePath"];
        //         string filePath = __path + __files;
                 
        //        if (!Directory.Exists(System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(filePath))))
        //        {
        //            Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filePath));
        //        }

              
                
        //        FileStream targetStream = null;
        //        using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
        //        {
        //            //read from the input stream in 4K chunks
        //            //and save to output stream
        //            const int bufferLen = 4096;
        //            byte[] buffer = new byte[bufferLen];
        //            int count = 0;
        //            while ((count =__Data.FileData.Read(buffer, 0, bufferLen)) > 0)
        //            {
        //                targetStream.Write(buffer, 0, count);
        //            }
        //            targetStream.Close();
        //            __Data.FileData.Close();
        //            //stream(entity);
        //        }
        //    }
           
        //    DirectoryInfo dir = new DirectoryInfo(path);
        //    IList<ProductPhotoEnitty> list = new List<ProductPhotoEnitty>();
        //    foreach (DirectoryInfo _dir in dir.GetDirectories())
        //    {
        //        ProductPhotoEnitty pEntity = new ProductPhotoEnitty();
        //        pEntity.SN = _dir.Name;
        //        pEntity.ProductName = _dir.Name;
        //        pEntity.ID = new Guid();
        //        list.Add(pEntity);
        //    }
        //    channel.UploadProduct(ZX.Common.Globals.ListToJson<ProductPhotoEnitty>(list));
            
        //    using (channel as IDisposable)
        //    {
        //        uFile(path, channel);
        //    }

        //}

        //static void uFile(string path, iSun.IUpLoadService channel)
        //{
        //    DirectoryInfo dir = new DirectoryInfo(path);

        //    FileInfo[] allFile = dir.GetFiles();
        //    foreach (FileInfo fi in allFile)
        //    {
        //        string _path = ConfigurationManager.AppSettings["filePath"];
        //        ProductImage entity = new ProductImage();
               
        //        entity.fileName = fi.Name;
        //        entity.FilePath = fi.FullName.Replace(_path, "").Replace(fi.Name,"");
        //        entity.SN = entity.FilePath.Split('\\')[1];
        //        entity.DirectoryName = fi.DirectoryName.Replace(_path, "");
                
        //        entity.StepN = 0;
                
        //        entity.SourceFileName = fi.Name;
        //        entity.FileType = fi.Extension;

        //        entity.ImageType = "0";
        //        entity.ImageType = entity.FilePath.Contains("\\thumb\\") ? "1" : entity.ImageType;
        //        entity.ImageType = entity.FilePath.Contains("\\vedio\\") ? "2" : entity.ImageType;
        //        //entity.ImageType = 
        //        if (fi.Extension.IndexOf("jpg") > -1)
        //        {
        //            Fotofly.JpgPhoto jpgPhotoOne = ZX.Common.IPTC.ReadImageInfo.Load(fi.FullName);
        //            entity.Summary = jpgPhotoOne.Metadata.Comment;
        //            entity.CreationSoftware = jpgPhotoOne.Metadata.CreationSoftware;
        //            entity.CameraManufacturer = jpgPhotoOne.Metadata.CameraManufacturer;
        //            entity.CameraModel = jpgPhotoOne.Metadata.CameraModel;
        //            entity.Copyright = jpgPhotoOne.Metadata.Copyright;
        //            entity.HeigthPix = jpgPhotoOne.Metadata.ImageHeight;
        //            entity.WidthPix = jpgPhotoOne.Metadata.ImageWidth;
        //        }
        //        string json = ZX.Common.Globals.ObjectToJSON(entity);

        //        iSun.FileUploadMessage file = new iSun.FileUploadMessage();
        //        file.SavePath = json;
        //        file.FileName = fi.Name;// "IMG_0467.jpg";
        //        file.FileData = new FileStream(fi.FullName, FileMode.Open);

        //        //UpLoadService.FileUploadMessage request = new UpLoadService.FileUploadMessage();
        //        //request.FileName = FileUpload1.FileName;
        //        //request.SavePath = "ooooooo";
        //        //request.FileData = FileUpload1.FileContent;
        //        //UpLoadService.IUpLoadService channel = ser.ChannelFactory.CreateChannel();
        //        channel.UploadFile(file);
        //        //channel.UploadFile(file);
        //        file.FileData.Close();
        //    }
        //    foreach (DirectoryInfo _dir in dir.GetDirectories())
        //    {
        //        uFile(_dir.FullName, channel);
        //    }
        //}
    }
}
